package com.fenqing.leetcode.code.t500_600;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.*;

public class Topic599 extends Topic {
    /**
     * StringArrayStringArrayStringArrayTwoTopicRvd
     * @param input1 input1
     * @param input2 input2
     */
    public Topic599(String input1, String input2) {
        super(input1, input2);
    }

    @Execute
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> hash = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            String s = list1[i];
            hash.put(s, i);
        }
        int min = Integer.MAX_VALUE;
        List<String> result = new ArrayList<>();
        for (int i = 0; i < list2.length; i++) {
            String s = list2[i];
            Integer integer = hash.get(s);
            if (integer != null && (integer + i) <= min) {
                if (integer + i == min) {
                    result.add(s);
                } else {
                    min = integer + i;
                    result.clear();
                    result.add(s);
                }
            }
        }
        return result.toArray(new String[]{});
    }

    public static void main(String[] args) {
        Topic599 topic599 = new Topic599("""
                ["Shogun", "Tapioca Express", "Burger King", "KFC"]
                """, """
                ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
                """);
        String[] run = topic599.run();
        System.out.println(Arrays.toString(run));
    }
}
